package com.taru.order.controller;

import com.taru.order.common.Data;
import com.taru.order.common.JsonResult;

import com.taru.order.common.LoginInfo;
import com.taru.order.entity.Articl;
import com.taru.order.entity.Colletion;
import com.taru.order.entity.Love;
import com.taru.order.entity.User;
import com.taru.order.service.UserService;
import com.taru.order.util.JwtUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户登录
 */
@Controller
public class UserController {
    @RequestMapping(value="/login",method = RequestMethod.POST)
    @ResponseBody
    public JsonResult login(@RequestBody User user){
        UserService userService=new UserService();
        JsonResult result=null;
        LoginInfo loginInfo=null;
        try {
            User users=userService.login(user.getUsername(),user.getPassword());
            if(users==null){
                result=new JsonResult(404,"账号密码错误");
            } else{
                String token= JwtUtil.createToken(user.getUser_id());
                loginInfo=new LoginInfo();
                loginInfo.setUser(users);
                loginInfo.setTokenHead("Bearer");
                loginInfo.setTokenHeaderName("Authorization");
                loginInfo.setToken(token);
                result=new JsonResult(200,"登录成功",loginInfo);
            }
        } catch (Exception e) {
            e.printStackTrace();
            result=new JsonResult(500,"服务器内部错误");
        }
        return result;
    }
    /**
     * 用户注册
     */
        @RequestMapping(value="/register",method = RequestMethod.POST)
        @ResponseBody
        public JsonResult register(@RequestBody User user){
            UserService userService=new UserService();
            JsonResult result=null;
            try {
                int isn=userService.register(user);
                if(isn!=1){
                    result=new JsonResult(404,"请输入正确格式");
                }
                else{
                    result=new JsonResult(200,"注册成功");
                }
            } catch (Exception e) {
                e.printStackTrace();
                result=new JsonResult(500,"服务器内部错误");
            }
            return result;
        }
    /**
     * 查询用户信息
     */
    @RequestMapping(value="/userinfo",method = RequestMethod.GET)
    @ResponseBody
    public JsonResult userInfo(String user_id ){
        UserService userService=new UserService();
        JsonResult result=null;
        try {
            User users=userService.userInfo(user_id);

                result=new JsonResult(200,"查询成功",users);

        } catch (Exception e) {
            e.printStackTrace();
            result=new JsonResult(500,"服务器内部错误");
        }
        return result;
    }
    /**
     * 修改用户信息
     */
    @RequestMapping(value="/changeuser",method = RequestMethod.PATCH)
    @ResponseBody
    public JsonResult changeHeader(@RequestBody User user){
        UserService userService=new UserService();
        JsonResult result=null;
        try {
            int isn=userService.changeUser(user);
            System.out.println(user.getUser_id()+","+user.getHeader());
            if(isn!=1){
                result=new JsonResult(404,"请输入正确格式");
            }
            else{
                result=new JsonResult(200,"修改成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result=new JsonResult(500,"服务器内部错误");
        }
        return result;
    }

    /**
     * 添加简介
     */
    @RequestMapping(value="/addbrief",method = RequestMethod.POST)
    @ResponseBody
    public JsonResult addBrief(@RequestBody User user){
        UserService userService=new UserService();
        JsonResult result=null;
        try {
            int isn=userService.addBrief(user.getUser_id(),user.getBrief());
            if(isn!=1){
                result=new JsonResult(404,"请输入正确格式");
            }
            else{
                result=new JsonResult(200,"添加成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result=new JsonResult(500,"服务器内部错误");
        }
        return result;
    }

    /**
     * 用户添加收藏
     */
        @RequestMapping(value="/collectarticl",method = RequestMethod.POST)
    @ResponseBody
    public JsonResult collect(@RequestBody Colletion collection){
        JsonResult result=null;
        UserService userService=new UserService();
        try {
            int isn=userService.collectArticl(collection.getUser_id(),collection.getArticl_id());
            if(isn!=1){
                result=new JsonResult(404,"请输入正确格式");
            }
            else{
                result=new JsonResult(200,"收藏成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result=new JsonResult(500,"服务器内部错误");
        }
        return result;
    }
    /**
     * 用户取消收藏文章
     */
    @RequestMapping(value="/nocollectarticl",method = RequestMethod.DELETE)
    @ResponseBody
    public JsonResult noCollectArticl(@RequestBody Colletion colletion){
        JsonResult result=null;
        System.out.println(colletion.getUser_id());
        System.out.println(colletion.getArticl_id());
        UserService userService=new UserService();
        try {
            int isn=userService.noCollectArticl(colletion.getUser_id(),colletion.getArticl_id());
            if(isn!=1){
                result=new JsonResult(404,"取消收藏失败");
            }
            else{
                result=new JsonResult(200,"取消收藏成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result=new JsonResult(500,"服务器内部错误");
        }
        return result;
    }

    /**
     * 查询收藏的文章
     */
        @RequestMapping(value="/selectcollect",method = RequestMethod.GET)
        @ResponseBody
        public JsonResult selectCollect(String user_id){
            UserService userService=new UserService();
            List<Articl> articls=null;
            JsonResult result=null;
            try {
                articls=userService.selectCollect(user_id);
                if(articls==null){
                    result=new JsonResult(404,"查询失败");
                }
                else{
                    result=new JsonResult(200,"查询成功",articls);
                }
            } catch (Exception e) {
                e.printStackTrace();
                result=new JsonResult(500,"服务器内部错误");
            }
            return result;
        }
    /**
     * 用户关注用户
     */
    @RequestMapping(value="/attentionuser",method = RequestMethod.POST)
    @ResponseBody
    public JsonResult collectUser(@RequestBody Love love){
        JsonResult result=null;
        UserService userService=new UserService();
        try {
            int isn=userService.attentionUser(love.getUser_id(),love.getLove_user_id());

            if(isn!=1 ){
                result=new JsonResult(404,"关注失败");
            }
            else{
                result=new JsonResult(200,"关注成功");
                userService.fansNumber(love.getLove_user_id());
            }
        } catch (Exception e) {
            e.printStackTrace();
            result=new JsonResult(500,"服务器内部错误");
        }
        return result;
    }
    /**
     * 用户取消关注用户
     */
    @RequestMapping(value="/noattentionuser",method = RequestMethod.DELETE)
    @ResponseBody
    public JsonResult noAttentionUser(@RequestBody Love love){
        JsonResult result=null;
        UserService userService=new UserService();
        try {
            int isn=userService.noAttentionUser(love.getUser_id(),love.getLove_user_id());
                if(isn!=1){
                result=new JsonResult(404,"取消关注失败");
            }
            else{
                result=new JsonResult(200,"取消关注成功");
                userService.fansNumberLose(love.getLove_user_id());
            }
        } catch (Exception e) {
            e.printStackTrace();
            result=new JsonResult(500,"服务器内部错误");
        }
        return result;
    }
    /**
     * 查询关注的用户
     */
    @RequestMapping(value="/selectlove",method = RequestMethod.GET)
    @ResponseBody
    public JsonResult selectLove(String user_id){
        UserService userService=new UserService();
        List<User> users=null;
        JsonResult result=null;
        try {
            users=userService.selectLove(user_id);
            if(users==null){
                result=new JsonResult(404,"查询失败");
            }
            else{
                result=new JsonResult(200,"查询成功",users);
            }
        } catch (Exception e) {
            e.printStackTrace();
            result=new JsonResult(500,"服务器内部错误");
        }
        return result;
    }
    /**
     * 查询粉丝量最多的前20个用户
     */
    @RequestMapping(value="/selectlovemax/{userId}",method = RequestMethod.GET)
    @ResponseBody
    public JsonResult selectLoveMax(@PathVariable("userId") String userId){
        UserService userService=new UserService();
        List<User> users=null;
        JsonResult result=null;
        try {
            users=userService.selectLoveMax(userId);
            if(users==null){
                result=new JsonResult(404,"查询失败");
            }
            else{
                result=new JsonResult(200,"查询成功",users);
            }
        } catch (Exception e) {
            e.printStackTrace();
            result=new JsonResult(500,"服务器内部错误");
        }
        return result;
    }
    /**
     * 分页查询用户
     */
    @RequestMapping(value="/users",method = RequestMethod.GET)
    @ResponseBody
    public JsonResult listUsers(Integer pageNum,Integer pageSize){
        UserService userService=new UserService();
        JsonResult result=null;
        try {
            Data data=userService.listUsers(pageNum,pageSize);
            if(data!=null){
                result=new JsonResult(200,"获取成功",data);
            }
            else{
                result=new JsonResult(404,"获取失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result=new JsonResult(500,"服务器内部错误");
        }
        return result;
    }

    /**
     * 判断用户关注用户
     */
    @RequestMapping(value="/users/love",method = RequestMethod.POST)
    @ResponseBody
    public JsonResult queryuserLike(@RequestBody Love love){
        UserService userService=new UserService();
        JsonResult result=null;
        try {
            boolean isExist=userService.queryuserLike(love);
            result=new JsonResult(200,"获取成功",isExist);
        } catch (Exception e) {
            e.printStackTrace();
            result=new JsonResult(500,"服务器内部错误");
        }
        return result;
    }

}
